package com.mstore.dao;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.mstore.pojo.User;

/**
 * 
 * 分店管理员
 * 
 */
public class UserDao extends SuperDao {

	public void save(User user) {
		super.save(user);
	}

	public void delete(User user) {
		super.delete(user);
	}

	public void updateUser(User user) {
		super.update(user);
	}

	public void update(User user) {
		/*
		 * String sql = "update b_user set loginName = '" + user.getLoginName()
		 * + "', mobile = '" + user.getMobile() + "', shop_id = " +
		 * user.getShopId() + " where id = " + user.getId();
		 * super.executeUpdate(sql);
		 */

		StringBuilder sb = new StringBuilder();
		sb.append("update b_user set loginName = '");
		sb.append(user.getLoginName());
		sb.append("', mobile = '");
		sb.append(user.getMobile());
		sb.append("', shop_id = ");
		sb.append(user.getShopId());
		sb.append(" where id = ");
		sb.append(user.getId());

		super.executeUpdate(sb.toString());
	}

	/**
	 * 
	 * 根据id获取分店信息
	 */
	public Object getOneUser(User user) {
		String sql = "from User where id=?";
		List<Object> object = super.getObject(sql,
				new Object[] { user.getId() });
		if (object.size() != 0) {
			return object.get(0);
		}
		else {
			return null;
		}
	}

	/**
	 * 根据id获取分店信息并抓取member
	 */
	public Object getOneUserAndMember(User user) {
		String sql = "from User u left join u.member where u.id=?";
		List<Object> object = super.getObject(sql,
				new Object[] { user.getId() });
		if (object.size() != 0) {
			return object.get(0);
		}
		else {
			return null;
		}
	}

	/**
	 * 根据标示符获取对应的分店
	 */
	public List<Object> getAllUser(User user) {
		String hql = "from User where paramKey=?";
		return super.getObject(hql, new String[] { user.getParamKey() });
	}

	/**
	 * 查出全部User
	 */
	public List<Object> getAllUser1() {
		String hql = "from User";
		return super.find(hql);
	}

	/**
	 * 
	 * 验证分店登录
	 */
	public Object findUser(User user) {
		String sql = "from User where loginName=? and password=? and paramKey=?";
		List<Object> objects = super.getObject(
				sql,
				new Object[] { user.getLoginName(), user.getPassword(),
						user.getParamKey() });
		if (objects.size() != 0) {
			return objects.get(0);
		}
		else {
			return null;
		}
	}

	/**
	 * 
	 * 根据Id修改密码
	 */
	public void updatePasswordById(User user) {
		/*
		 * String sql = "update  b_user  set password='" + user.getPassword() +
		 * "' where  id=" + user.getId(); super.executeUpdate(sql);
		 */

		StringBuilder sb = new StringBuilder();
		sb.append("update  b_user  set password='");
		sb.append(user.getPassword());
		sb.append("' where  id=");
		sb.append(user.getId());
		super.executeUpdate(sb.toString());
	}

	/**
	 * 修改标示符
	 */
	public void updateParamKey(User user) {
		/*
		 * String sql = "update  b_user  set paramKey='" + user.getParamKey() +
		 * "' where  id=" + user.getId(); super.executeUpdate(sql);
		 */

		StringBuilder sb = new StringBuilder();
		sb.append("update  b_user  set paramKey='");
		sb.append(user.getParamKey());
		sb.append("' where  id=");
		sb.append(user.getId());
		super.executeUpdate(sb.toString());
	}

	/**
	 * 
	 * 对用户名验证
	 */
	public boolean checkLoginName(User user) {
		String sql = "from User where loginName=?";
		List<Object> object = super.getObject(sql,
				new Object[] { user.getLoginName() });
		if (object.size() > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 
	 * 对电话号码的验证
	 */
	public boolean checkMobile(User user) {
		String sql = "from User where mobile=?";
		List<Object> object = super.getObject(sql,
				new Object[] { user.getMobile() });
		if (object.size() > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 
	 * 修改信息
	 */
	public void updateMessage(User user) {
		/*
		 * String sql = "update  b_user  set loginName='" + user.getLoginName()
		 * + "',  password='" + user.getPassword() + "'  ,  mobile='" +
		 * user.getMobile() + "' where  id=" + user.getId();
		 * super.executeUpdate(sql);
		 */

		StringBuilder sb = new StringBuilder();
		sb.append("update  b_user  set loginName='");
		sb.append(user.getLoginName());
		sb.append("',  password='");
		sb.append(user.getPassword());
		sb.append("'  ,  mobile='");
		sb.append(user.getMobile());
		sb.append("' where  id=");
		sb.append(user.getId());
		super.executeUpdate(sb.toString());
	}

	/**
	 * 当密码为空时修改
	 * 
	 */
	public void updateMessagePassword(User user) {
		/*
		 * String sql = "update  b_user  set loginName='" + user.getLoginName()
		 * + "' ,  mobile='" + user.getMobile() + "' where  id=" + user.getId();
		 * super.executeUpdate(sql);
		 */

		StringBuilder sb = new StringBuilder();
		sb.append("update  b_user  set loginName='");
		sb.append(user.getLoginName());
		sb.append("' ,  mobile='");
		sb.append(user.getMobile());
		sb.append("' where  id=");
		sb.append(user.getId());
		super.executeUpdate(sb.toString());

	}

	/**
	 * 根据member_id查找
	 */
	public Object findUserByMemberId(User user) {
		/*
		 * String hql = "from User u where u.member.id = " +
		 * user.getMember().getId();
		 */

		StringBuilder sb = new StringBuilder();
		sb.append("from User u where u.member.id = ");
		sb.append(user.getMember().getId());
		List<Object> objects = super.find(sb.toString());
		if (objects != null && objects.size() > 0)
			return objects.get(0);
		else
			return null;
	}

	/**
	 * 查找member_id
	 */
	public Object getMemberIdFromUser(Long id) {
		/*
		 * String sql="SELECT b.member_id  FROM  b_user b WHERE b.id=" + id;
		 * return super.executeSql(sql);
		 */

		StringBuilder sb = new StringBuilder();
		sb.append("SELECT b.member_id  FROM  b_user b WHERE b.id=");
		sb.append(id);
		return super.executeSql(sb.toString());
	}

	/**
	 * 查找除自己以外相同手机号的member的个数
	 */
	public Long getCountMemberByMobileOutSelf(User user) {
		String separate = " where ";
		StringBuilder sb = new StringBuilder();
		sb.append("select count(id) from Member");

		Long id = user.getMember().getId();
		if (id != null) {
			sb.append(separate);
			sb.append("id != ");
			sb.append(id);
			separate = " and ";
		}

		String mobile = user.getMobile();
		if (StringUtils.isNotBlank(mobile)) {
			sb.append(separate);
			sb.append("mobile = '");
			sb.append(mobile);
			sb.append("'");
			separate = " and ";
		}

		List<Object> list = find(sb.toString());
		if (list.isEmpty()) {
			return 0l;
		}

		return (Long) list.get(0);
	}

}
